package net.lovesrc.allin.biz.sysconfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.ImmutableBiMap;
import net.lovesrc.allin.Message;
import net.lovesrc.allin.biz.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys-config")
public class SysConfigController extends BaseService {

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping
    public Message get() {
        JSONObject obj = sysConfigService.getConfigs();
        String str = JSON.toJSONString(obj, SerializerFeature.PrettyFormat, SerializerFeature.MapSortField);
        return Message.success(
                ImmutableBiMap.of("obj", obj, "str", str)
        );
    }

    @PostMapping("/save")
    public Message save(String configs) {
        sysConfigService.saveConfigs(configs);
        return Message.success();
    }

    @PostMapping("/shutdown")
    public Message shutdown() {
        System.exit(0);
        return Message.success();
    }

}
